//
//  EarningsViewController.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/18.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "EarningsViewController.h"
#import "WKWebViewJavascriptBridge.h"
#import "MainViewController.h"

#import "HTAssembleKey.h"
#import "WXAuth2ViewController.h"
#import "AppUtil.h"
@interface EarningsViewController ()<UIWebViewDelegate>
{
    NSString *html5Url;
}

@property (strong,nonatomic) WKWebView *webView;
@property WKWebViewJavascriptBridge * bridge;
@end

@implementation EarningsViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [self hideNavigationBar];
    html5Url = [NSString stringWithFormat:@"%@%@",Http_H5_URL,@"index.html#/income"];
    [self loadUrl:html5Url];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubViews];
    [self webviewSets];
    
}

#pragma mark 添加view
-(void)addSubViews{
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    [self.view addSubview:_webView];
}

#pragma mark webview配置
-(void)webviewSets{
    if(_webView){
        _bridge = [WKWebViewJavascriptBridge bridgeForWebView:_webView];
        [_bridge registerHandler:@"getSign" handler:^(id data, WVJBResponseCallback responseCallback) {
            responseCallback(HTCFUDFGetValue(kSignServiceKey));
        }];
        //前往排行榜
        [_bridge registerHandler:@"gotoRank" handler:^(id data, WVJBResponseCallback responseCallback) {
            _webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            html5Url = [NSString stringWithFormat:@"%@%@",Http_H5_URL,@"index.html#/rank"];
            [self loadUrl:html5Url];
            MainViewController *mainCtrol = (MainViewController *)self.tabBarController;
            [mainCtrol hiddenTabBarWeb];
        }];
        //回退
        [_bridge registerHandler:@"goback" handler:^(id data, WVJBResponseCallback responseCallback) {
            MainViewController *mainCtrol = (MainViewController *)self.tabBarController;
            [mainCtrol showTabBarWeb];
            _webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64);
            html5Url = [NSString stringWithFormat:@"%@%@",Http_H5_URL,@"index.html#/income"];
            [self loadUrl:html5Url];
            
        }];
        //新的accessToken过来了
        [_bridge registerHandler:@"saveSign" handler:^(id data, WVJBResponseCallback responseCallback) {
            NSString *newaccessToken = [data objectForKey:@"accessToken"];
            NSLog(@"zee--newaccessToken: %@",newaccessToken);
            HTCFUDFCache(kAccessTokenKey,newaccessToken);
            accessToken = HTCFUDFGetValue(kAccessTokenKey);
            NSString *encryptionStr = [NSString stringWithFormat:@"%@|%d|%@|%@",accessToken,userId,refreshToken,IDFV];
            NSLog(@"encryptionStr :%@",encryptionStr);
            
            NSString *privateKey = HTCFAES128EncryptUtil(encryptionStr,signKey);
            NSLog(@"zee--privateKey:%@",privateKey);
            HTCFUDFCache(kSignServiceKey,privateKey);
            encryptionToken = privateKey;
            
        }];
        
        [_bridge registerHandler:@"errorCode" handler:^(id data, WVJBResponseCallback responseCallback) {
            MainViewController *mainCtrol = (MainViewController *)self.tabBarController;
            [mainCtrol showTabBarWeb];
            _webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64);
//            [self loadUrl:@"http://192.168.1.239:8085/static/happySell/index.html#/income"];
            
            [[HTAssembleKey sharedTool] clearKey];
            authorizationCode = HTCFUDFGetValue(kAuthorizationKey);
            NSLog(@"%@",authorizationCode);
            if(HTCFStringIsNull(authorizationCode)){
                WXAuth2ViewController *wxAuth2VC = [[WXAuth2ViewController alloc] init];
                [self presentViewController:wxAuth2VC animated:YES completion:nil];
            }
        }];
        //
        [_bridge registerHandler:@"htmlToast" handler:^(id data, WVJBResponseCallback responseCallback) {
            [AppUtil showTipMessage:[data objectForKey:@"str"]];
        }];
    }
}

#pragma mark 加载url
-(void)loadUrl:(NSString *)urlStr{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
